// pages/musicInfo/musciInfo.js
import login from '../../utils/login'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasSource:0,
    cover:'',
    musicList:[],
    title:'',
    isLogin:false,
    musicPlay:false,
    mapOfMusic:null,
    musics:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    wx.request({
      url: 'https://thinker.skyseaee.com/initMusic',
      success (res) {
        that.setData({
          musicList:res.data.data
        })
      }
    })
    const orderID = wx.getStorageSync('lastMusicId') || ''
    if(orderID.length>1) {
      this.setData({
        hasSource:3
      })
      app.globalData.currentMusicId = orderID
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let hasSource = app.globalData.hasSource
    let login = app.globalData.isLogin
    // console.log(login)
    if(login) {
      this.setData({
        isLogin:login
      })
    }
    this.setData({
      hasSource:hasSource
    })
    let that = this
    setTimeout(function(){
      let musics = app.globalData.musics
      that.setData({
        musics:musics
      })  
    }, 1000)

    if(this.data.hasSource==1) {
      this.updateData()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    if(this.data.musicPlay)
      this.pauseorPlayMusic()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  nav2Community: function () {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },

  nav2CreateZone: function () {
    wx.navigateTo({
      url: '/pages/portfolio/portfolio',
    })
  },

  loginFun: function () {
    login.loginFun(this)
    app.globalData.isLogin = true
  },

  playControl: function () {
    // TODO: control music play or pause
    this.pauseorPlayMusic()
  },

  // TODO: play music
  initMusic: function() {
    // console.log('init music')
    const music = this.data.musicList
    // console.log(music)
    const musicMap = new Map()
    for(let mu of music) {
      let innerAudioContext = wx.createInnerAudioContext()
      innerAudioContext.src = 'https://thinker.skyseaee.com/audio/'+mu.url.split('/')[2]
      // console.log('https://thinker.skyseaee.com/audio/'+mu.url.split('/')[2])
      innerAudioContext.autoplay = true
      innerAudioContext.loop = true
      innerAudioContext.volume = Math.floor(mu.auto)/600
      musicMap.set(mu.id,innerAudioContext)
    }
    this.setData({
      mapOfMusic:musicMap,
      musicPlay:true
    })
  },

  pauseorPlayMusic: function() {
    let played = this.data.musicPlay
    let mapOfMusic = this.data.mapOfMusic
    // 播放->暂停状态
    if(played) {
      for(let innerAudioContext of mapOfMusic.values()) {
        innerAudioContext.pause()
      }
    } else {
      for(let innerAudioContext of mapOfMusic.values()) {
        innerAudioContext.play()
      }
    }
    this.setData({
      musicPlay:!played
    })
  },

  switchNext: function () {
    let music = this.data.musics
    let index = music.indexOf(app.globalData.currentMusicId)
    if(index==music.length-1) {
      index=0
    } else {
      index+=1
    }
    app.globalData.currentMusicId = music[index]
    wx.setStorage({
      data: app.globalData.currentMusicId,
      key: 'lastMusicId',
    })
    // 重新刷新界面
    this.updateData()
  },

  switchLast: function () {
    let music = this.data.musics
    let index = music.indexOf(app.globalData.currentMusicId)
    if(index==0) {
      index=music.length-1
    } else {
      index-=1
    }
    app.globalData.currentMusicId = music[index]
    wx.setStorage({
      data: app.globalData.currentMusicId,
      key: 'lastMusicId',
    })
    // 重新刷新界面
    this.updateData()
  },

  updateData: function() {
    let orderID = app.globalData.currentMusicId
    let that = this
    // console.log(orderID)
    wx.setStorage({
      data: orderID,
      key: 'lastMusicId',
    })
    wx.request({
      url: 'https://thinker.skyseaee.com/getMusicInfo',
      data:{
        orderID:orderID
      },
      success(res){
        //musicList = res.data.data
        let musciAuto = JSON.parse(res.data.data[0].info)
        let musicList = that.data.musicList
        // console.log(musicList)
        for(let music of musicList) {
          // console.log('id',music.id,'auto',musciAuto[music.id])
          music.auto = parseInt(musciAuto[music.id]) || 0
        }
        let cover = res.data.data[0].cover
        let tmpPath = cover.split('.')[1]
        that.setData({
          musicList:musicList,
          cover:"https://thinker.skyseaee.com/initCover/?fileName="+orderID+'.'+tmpPath,
          title:res.data.data[0].title
        })
        // console.log(musicList)
        that.initMusic()
      }
    })
    app.globalData.hasSource = 3
  },

  swicthPort: function() {
    console.log('pip')
    wx.navigateTo({
      url: '/pages/portfolio/portfolio',
    })
  }
})